package com.fesine.crm.controller;

import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import com.fesine.crm.exception.CrmException;
import com.fesine.crm.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/20
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/20
 */
@RestController
@RequestMapping("/v1/file")
public class FileUploadController {
    @Autowired
    private FastDFSClient dfsClient;

    @PostMapping("")
    public Result uploadFile(@RequestParam MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            try {
                String path = dfsClient.uploadFile(file);
               return ResultUtils.success(path);
            } catch (IOException e) {
                e.printStackTrace();
                throw new CrmException(ResultEnum.INVALID_REQUEST.getCode(), file.getName());
            }
        }
        return ResultUtils.error(ResultEnum.INVALID_REQUEST.getCode(), file.getName());
    }

    @GetMapping("")
    public void getFile(@RequestParam String path, HttpServletResponse response) {

    }

    @DeleteMapping("")
    public void deleteFile(@RequestParam String path, HttpServletResponse response) {
        dfsClient.deleteFile(path);

    }

}
